package com.cnap.db.mapper;

import cn.hutool.core.util.IdUtil;
import com.cnap.CnapApplicationTests;
import com.cnap.db.entity.ApplicationPO;
import com.cnap.db.entity.IndicatorCasePO;
import com.cnap.db.entity.IndicatorPO;
import com.cnap.db.entity.MicroServicePO;
import com.cnap.model.application.QueryApplicationParam;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <类文件描述>
 *
 * @author xc
 **/
//@Transactional
class IndicatorMapperTest extends CnapApplicationTests {

    @Autowired
    private IndicatorMapper indicatorMapper;

    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private MicroServiceMapper microServiceMapper;

    /**
     * 当指标有更新时，更新指标实例，需要注释上面的@Transactional
     * 需要先清空indicator和indicator_case表，再插入新的指标，再运行本方法
     */
    //@Test
    void update() {
        QueryApplicationParam queryApplicationParam = new QueryApplicationParam();
        queryApplicationParam.setPageSize(100);
        queryApplicationParam.setPageNum(1);
        queryApplicationParam.setApplicationName("");
        queryApplicationParam.setCategory("");
        List<ApplicationPO> applicationPOS = applicationMapper.selectApplicationByParam(queryApplicationParam);

        for (ApplicationPO applicationPO : applicationPOS) {
            List<IndicatorPO> indicatorPOS = indicatorMapper.selectByCategoryAndType(applicationPO.getCategory(),
                    applicationPO.getType());

            List<MicroServicePO> microServicePOS = microServiceMapper.selectMicroServiceByAppId(applicationPO.getId());

            for (MicroServicePO po : microServicePOS) {
                List<IndicatorCasePO> indicatorCasePOS = indicatorPOS.stream().map(indicatorPO -> {
                    IndicatorCasePO indicatorCasePO = new IndicatorCasePO();
                    indicatorCasePO.setId(IdUtil.simpleUUID());
                    indicatorCasePO.setAppId(applicationPO.getId());
                    indicatorCasePO.setSvcId(po.getId());
                    indicatorCasePO.setSvcName(po.getMicroServiceName());
                    indicatorCasePO.setIndicatorId(indicatorPO.getId());
                    indicatorCasePO.setValue(indicatorPO.getValue());
                    return indicatorCasePO;
                }).collect(Collectors.toList());
                int s= indicatorMapper.batchInsertIndicatorCases(indicatorCasePOS);
            }


        }
    }
}